package com.bw.repair.net

import androidx.lifecycle.MutableLiveData
import com.bw.common.vm.BaseViewModel
import com.bw.http.utils.zgr.OkHttpClientManager
import com.bw.repair.entity.RepairEntity
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class RepairViewModel:BaseViewModel() {



    val repairSuccess by lazy { MutableLiveData<List<RepairEntity>>() }
    val postRepairSuccess by lazy { MutableLiveData<RepairEntity>() }
    val deleteRepairSuccess by lazy { MutableLiveData<String>() }
    val uploadFileSuccess by lazy { MutableLiveData<String>() }


    private val apiService: ApiService = OkHttpClientManager.retrofitInstance.create(ApiService::class.java)

    fun getRepair() {

        executeNetwork(
            { apiService.getRepair() },
            repairSuccess::postValue
        )
    }

    fun postRepair(repairEntity: RepairEntity) {
        executeNetwork(
            { apiService.postRepair(repairEntity) },
            postRepairSuccess::postValue
        )
    }

    fun deleteRepair(id:Int) {
        executeNetwork(
            { apiService.deleteRepair(id) },
            deleteRepairSuccess::postValue
        )
    }

    fun uploadFile(
        file: File,
        modulename: String,
        userid: String
    ) {

        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, body)

        executeNetwork(
            { apiService.uploadFile(filePart,modulename, userid) },
            uploadFileSuccess::postValue
        )
    }




}